<?php
namespace app\admin\controller;

use app\admin\validate\Admin as AdminValidate;
use app\admin\model\Admin as AdminModel;
use app\common\exception\AdminException;

class Admin extends Base
{
    protected function _initialize()
    {
        //如果要检测token,就调用父类_initialize方法
        parent::_initialize();
    }

    public function index()
    {
        $admin = AdminModel::select();
        if($admin->isEmpty()){
            throw new AdminException();
        }
        $data['data'] = $admin->toArray();
        $data['total'] = $admin->count();
        $this->ok($data);
    }

    public function save()
    {
        (new AdminValidate())->scene('save')->goCheck();
        $params = $this->request->param();
        $data['username'] = $params['username'];
        $data['password'] = md5($params['password']);
        $data['status'] = 1;
        $admin = AdminModel::create($data, true);
//        $data = AdminModel::get($admin->id)->toArray();
        $this->ok($admin->toArray(), '创建管理员成功');
    }

    public function read($id)
    {

    }

    public function update($id)
    {

    }

    public function delete($id)
    {

    }

    public function upload()
    {
        return $this->upload_img('admin');
    }

    public function uploads()
    {
        return $this->upload_imgs('admin');
    }
}